Fixes the first stbi_load_gif issue in #1838 by clearing delays on all outofmem paths
#1839
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
After calling
stbi_load_gif, apps should free thedelayspointer it passed in ifdelaysis non-null. This means that after freeingdelaysin thestbi__load_gif_main_outofmemerror path, stb_image_load should also setdelaystoNULL. Otherwise if we allocatedelaysonce but we get tostbi__load_gif_main_outofmemon a later frame, then the app sees a non-nulldelayspointer and tries to free it again.With this change, running
clang++ poc.c -fsanitize=address && ./a.out 490442704-000e388f-3edd-409b-9253-83046ac80317.giffrom #1838 now correctly reports "Failed to load GIF" instead of segfaulting.Forks with JarLob's double-free fix in #1549 were also affected; it implemented this logic on some paths, but not all, and #1838 happens to reach one of the other paths. Moving the
delays = NULLlogic tostbi__load_gif_main_outofmemon those forks allows us to save a few lines of code.In addition, since
reallocfirst frees the input pointer (C specification 7.22.3.5 item 1), we shouldn't pass the input tostbi__load_gif_main_outofmemafter a realloc -- then we'd get a different double-free. If we remove the temporary pointer and dop = realloc(p, ...)then I think we get the right behavior and save a few lines of code.Thanks!